﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Net.Mail;
using System.Net;

namespace MIMDesignShaft
{
    public partial class EmailValidation : DevExpress.XtraEditors.XtraForm
    {
        //定义一个全局的string类型的验证码
        string code = "";
        //定义一个字符创，里面包含所需要的验证码元素
        string a = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        //定义一个时间，用于倒计时
        int time;
        //邮箱
        string user = "";
        string userName = "";
        string userPhone = "15566285282";
        delegate void SetValue();
        public EmailValidation()
        {
            InitializeComponent();
        }
        public EmailValidation(string name,string phone)
        {
            InitializeComponent();
            userName = name;
            userPhone = phone;
        }
        private void sendHyperlinkLabelControl_Click(object sender, EventArgs e)
        {
            code = "";
            //实例化一个随机数
            Random b = new Random();
            //循环6次得到一个随机的六位数的验证码
            for (int i = 0; i < 6; i++)
            {
                code = code + a.Substring(b.Next(0, a.Length), 1);
            }
            //创建服务器对象
            SmtpClient smtp = new SmtpClient("smtp.163.com");
            //smtp.EnableSsl = true;
            //创建邮件对象准备发送
            MailAddress mail1 = new MailAddress("cmic_miningcloud@163.com");

            try
            {
                //获取文本框的收件人的邮箱
                user = emailTextEdit.Text + "@enfi.com.cn";
                MailAddress mail2 = new MailAddress(user);
                //创建邮件对象，准备发送【mail1是发件人地址，mail2是收件人地址】
                MailMessage mess = new MailMessage(mail1, mail2);
                //邮件的标题
                mess.Subject = "井建系统验证码";
                //邮件的内容
                mess.Body = "【井建计算】验证码：" + code + "。您正在使用登录验证功能，该验证码仅用于身份验证，请勿泄露给他人使用。系统邮件请勿回复！";
                //创建互联网安全证书
                NetworkCredential cred = new NetworkCredential("cmic_miningcloud@163.com", "RBDKKPTULOUARLFK");
                //NetworkCredential cred = new NetworkCredential("3506728732@qq.com", "zjloxfaiqjvidbjf");
                //证书绑定到服务器对象以便服务器验证
                smtp.Credentials = cred;
                //开始发送
                smtp.Send(mess);
                ShowTime();
            }
            catch(Exception ex)
            {
                MessageBox.Show("输入正确的邮箱格式！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }
        public void ShowTime()
        {
            sendHyperlinkLabelControl.Enabled = false;
            //激活timer事件
            timer1.Enabled = true;
            //倒计时60秒
            time = 60;
        }

        private void comfirmSimpleButton_Click(object sender, EventArgs e)
        {
            if (emailTextEdit.Text == "")
            {
                MessageBox.Show("输入正确的邮箱！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                emailTextEdit.Focus();
                return;
            }
            if (codeTextEdit.Text == code && codeTextEdit.Text != "")
            {
                string nowTime = DateTime.Now.ToString();
                string sql = string.Format("insert into MIMDesignShaft_VerifiedUser(verifiedPhoneNumber,verifiedEmail,verifiedTime) values('{0}','{1}','{2}')", userPhone, user, nowTime);
                int count = SqlHelper.ExecuteNonQuery(sql, null);
                if (count > 0)
                {
                    //跳转到主界面
                    this.Hide();
                    Form1 mainForm = new Form1(userName);
                    mainForm.ShowDialog();
                }
            }
            else
            {
                MessageBox.Show("验证码不正确！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                codeTextEdit.Focus();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            time--;
            sendHyperlinkLabelControl.Text = time+ "s后重新发送";
            if (time == 0)
            {
                sendHyperlinkLabelControl.Enabled = true;
                sendHyperlinkLabelControl.Text = "重新发送验证码";
                //关闭timer事件
                timer1.Enabled = false;
                //倒计时60秒
                time = 60;
            }
        }

        private void EmailValidation_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}